﻿<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<div class="easyui-layout" id="rightnorthID" style="width:100%;height:100%;padding:0px;">
	<div region="north" split="false" style="border:none;height:30px;background:#dbeaf9;overflow:hidden">
			<div style="width:100%;padding-top:2px"> 
				<div style="float:left;padding-left:5px"><a href="javascript:void(0);" onclick="getWeekPlanDetail(lastweekstart);" class="easyui-linkbutton" iconCls="icon-back-oval" plain="true"></a></div>
				<div style="float:left;"><a href="javascript:void(0);" onclick="getWeekPlanDetail(nextweekstart);" class="easyui-linkbutton" iconCls="icon-next" plain="true"></a></div>
				<div id="daterange" style="float:left;padding-top:5px;padding-left:5px">&nbsp;</div>	 
			</div>		
			<div id="rightnorthright" style="float:right;">
				<div style="float:left;padding-right:5px"><a href="javascript:void(0);" onclick="getWeekPlanDetail(thisweekstart);" class="easyui-linkbutton" iconCls="icon-reload" plain="true"></a></div>	
			</div>
	</div>
	<div region="center" style="padding:0px;border:none;overflow:auto">
		<table id="tb"></table>		
	</div>
</div>
<script>	
	/**
		制定周计划表格
	*/
	$('#tb').datagrid({
		height:400,
		nowrap: false,
		autoRowHeight: false,
		striped: true,
		singleSelect:true,
		fitColumns:true,
		rownumbers:true,
		//url:'datagrid_data.json',
		columns:[[
			{field:'orgid',title:'客户ID',hidden:true},
			{field:'orgcode',title:'客户代码',width:50},
			{field:'orgname',title:'客户名称',width:200},
			{field:'sun',title:'周日',width:40,align:'center',
				formatter:function(value,row,index){
					var disabledstr = checkDays(0);		
					var str = "<input onclick='save(0,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox'>";
					if(value=="1"){
						str = "<input onclick='save(0,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox' checked>";
						copyflag = 'true';
					}
					return str;
				}
			},
			{field:'mon',title:'周一',width:40,align:'center',
				formatter:function(value,row,index){
					var disabledstr = checkDays(1);								
					var str = "<input onclick='save(1,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox'>";
					if(value=="1"){
						str = "<input onclick='save(1,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox' checked>";
						copyflag = 'true';
					}
					return str;
				}
			},
			{field:'tue',title:'周二',width:40,align:'center',
				formatter:function(value,row,index){
					var disabledstr = checkDays(2);		
					var str = "<input onclick='save(2,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox'>";
					if(value=="1"){
						str = "<input onclick='save(2,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox' checked>";
						copyflag = 'true';
					}
					return str;
				}
			},
			{field:'wed',title:'周三',width:40,align:'center',
				formatter:function(value,row,index){
					var disabledstr = checkDays(3);		
					var str = "<input onclick='save(3,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox'>";
					if(value=="1"){
						str = "<input onclick='save(3,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox' checked>";
						copyflag = 'true';
					}
					return str;
				}
			},
			{field:'thu',title:'周四',width:40,align:'center',
				formatter:function(value,row,index){
					var disabledstr = checkDays(4);		
					var str = "<input onclick='save(4,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox'>";
					if(value=="1"){
						str = "<input onclick='save(4,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox' checked>";
						copyflag = 'true';
					}
					return str;
				}
			},
			{field:'fri',title:'周五',width:40,align:'center',
				formatter:function(value,row,index){
					var disabledstr = checkDays(5);		
					var str = "<input onclick='save(5,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox'>";
					if(value=="1"){
						str = "<input onclick='save(5,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox' checked>";
						copyflag = 'true';
					}
					return str;
				}
			},
			{field:'sat',title:'周六',width:40,align:'center',
				formatter:function(value,row,index){
					var disabledstr = checkDays(6);		
					var str = "<input onclick='save(6,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox'>";
					if(value=="1"){
						str = "<input onclick='save(6,"+ row.orgid +",this)' "+ disabledstr +" type='checkbox' checked>";
						copyflag = 'true';
					}
					return str;
				}
			}
		]]
	});
	/**
		根据日期获取周计划初始数据
	*/
	getWeekPlanDetail(ovaldateStr);
	
	function getWeekPlanDetail(dateparam){
		paramsstr = "{'cudate':'"+dateparam+"','employeeid':"+oval_user_search+"}";
		var variable = new Array('__methodName', '__token', 'pagename', 'actionname','__params');
		var value = new Array('wsActionJSON', token, 'p_visitplan', 'getvisitplanweekdetail',paramsstr);
		var dataStr = getJsonData(variable, value);
		connectionServlet(dataStr, getWeekPlanDetailCallback);
	}	
	
	function getWeekPlanDetailCallback(data){
		if(data.successflag=="true"){
			var rangeArray = data.result.dates.weekrange.split("~");
			thisweekrange = rangeArray[0]+"&nbsp;&nbsp;~&nbsp;&nbsp;"+rangeArray[1];
			$('#daterange').html(thisweekrange);
			lastweekstart = data.result.dates.lastweekstart;
			nextweekstart = data.result.dates.nextweekstart;
			thisweekstart = data.result.dates.thisweekstart;
			copyflag="false";
			$('#tb').datagrid('loadData',{                                                                     
				"rows":data.result.rows                                                          
			});	
			if(data.result.copyflag=="true" && copyflag=="true"){				
				$('#copyWeekPlanBtn').linkbutton('enable');
			}else{
				$('#copyWeekPlanBtn').linkbutton('disable');
			}
		}
	}
	/**
		检查该列是否在有效编辑日期内
	*/
	function checkDays(days){
			var array = thisweekstart.split("-");
			var date1 = new Date(array[0],array[1],array[2]);
			var thisday_milliseconds = eval(date1.getTime() + "+" + 1000*60*60*24*days);
            var today = new Date();
            var strYear = today.getFullYear();
            var strDay = today.getDate();
            var strMonth = today.getMonth()+1;
            datastr = strYear+"-"+strMonth+"-"+strDay;
			var date2 = new Date(strYear,strMonth,strDay);
			var today_milliseconds = date2.getTime();
			if(today_milliseconds>thisday_milliseconds)
				return "disabled='true'";
			else
				return "";
    }
	/**
		为选中项生成、取消计划
	*/
	function save(weeknum,idnum,obj){
		var operate = 'delete';
		if(obj.checked){
			operate = 'insert';
		}
		object = obj;
		var paramsstr = "{'employeeid':"+oval_user_search+",'orgid':"+idnum+",'planvisitdate':'"+thisweekstart+"','operate':'"+operate+"','weeknum':"+weeknum+"}";
		var variable = new Array('__methodName', '__token', 'pagename', 'actionname','__params');
		var value = new Array('wsActionJSON', token, 'p_visitplan', 'savevisitplandetail',paramsstr);
		var dataStr = getJsonData(variable, value);
		connectionServlet(dataStr, saveDataSuccess);
	}
	
	function saveDataSuccess(data){
		if(data.successflag=="false"){
			if(data.result.errorcode[0].messagecode == "P_0063"){
				$.messager.alert('提示','已有拜访反馈不能取消计划！','info');
				object.checked = true;
			}
		}
	}
	/**
		周计划“模板”复制
	*/
	function copyWeekPlanTemp(){		
		$.messager.confirm('提示','确定复制'+thisweekrange+'的计划到本周吗?', function(r){
			if (r){
				var paramsstr = "{'employeeid':"+oval_user_search+",'thisweekstart':'"+thisweekstart+"'}";
				var variable = new Array('__methodName', '__token', 'pagename', 'actionname','__params');
				var value = new Array('wsActionJSON', token, 'p_visitplan', 'copy2visitplan',paramsstr);
				var dataStr = getJsonData(variable, value);
				connectionServlet(dataStr, copyWeekPlanTempSuccess);
			}
		});	
	}
	
	function copyWeekPlanTempSuccess(data){
		if(data.successflag=="false"){
			if(data.result.errorcode[0].messagecode == "P_0063"){
				$.messager.alert('提示','本周已有计划，不能复制！','info');
				object.checked = true;
			}
		}else{		
			$.messager.show({
				title:'提示',
				msg: thisweekrange+'的计划复制成功！',
				timeout:4000,
				showType:'slide'
			});
			getWeekPlanDetail(ovaldateStr);		
		}
	}
</script>
<body>
<html>